package com.djmw.mouse.web.account;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.djmw.mouse.entity.AdminUser;
import com.djmw.mouse.entity.User;
import com.djmw.mouse.service.account.AdminAccountService;

/**
 * 管理员管理用户的Controller.
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/admin/user")
public class UserAdminController {

    @Autowired
    private AdminAccountService adminAccountService;

    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        List<AdminUser> users = adminAccountService.getAllUser();
        model.addAttribute("users", users);

        return "admin/user/admin-user-list";
    }

    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("user", adminAccountService.getUser(id));
        return "admin/user/admin-user-profile";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute("preloadUser") User user, RedirectAttributes redirectAttributes) {
        adminAccountService.updateUser(user);
        redirectAttributes.addFlashAttribute("message", "更新用户" + user.getLoginName() + "成功");
        return "redirect:/admin/user";
    }

    @RequestMapping(value = "register", method = RequestMethod.GET)
    public String registerForm() {
        return "admin/user/admin-user-register";
    }

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public String register(@Valid AdminUser user, RedirectAttributes redirectAttributes) {
        adminAccountService.registerUser(user);
        redirectAttributes.addFlashAttribute("message", "注册用户" + user.getLoginName() + "成功");
        return "redirect:/admin/user";
    }

    @RequestMapping(value = "delete/{id}")
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        AdminUser user = adminAccountService.getUser(id);
        adminAccountService.deleteUser(id);
        redirectAttributes.addFlashAttribute("message", "删除用户" + user.getLoginName() + "成功");
        return "redirect:/admin/user";
    }

	            /**
     * 使用@ModelAttribute, 实现Struts2
     * Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
     * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
     */
    @ModelAttribute("preloadUser")
    public User getUser(@RequestParam(value = "id", required = false) Long id) {
        if (id != null) {
            return adminAccountService.getUser(id);
        }
        return null;
    }
}
